﻿using MultiAxisAssembly;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZRFCore.MotionControlCard;
using ZRFCore.ToolForm;

namespace 喷点胶机
{
    public partial class Page_4_IOTable : UIPage
    {
        ControlMaster controlMaster;
        public IInput[] Inputs;
        public IOutput[] Outputs;
        //private Type thisType;
        public Page_4_IOTable(ControlMaster controlMaster)
        {
            InitializeComponent();
            this.controlMaster = controlMaster;
            this.Inputs = controlMaster.Inputs;
            this.Outputs = controlMaster.Outputs;
            this.KeyDown += controlMaster.MulitAxisMasterControl.KeyDown;
            this.KeyUp += controlMaster.MulitAxisMasterControl.KeyUp;
            foreach (var control in this.Controls)
            {
                if (control is UITextBox)
                {
                    ((UITextBox)control).GotFocus += TextInputGotFocus;
                    ((UITextBox)control).KeyPress += ZRFCore.Tool.ZrfFormControls.TextBoxNumValOnly_KeyPress;
                }
            }
        }

        private void TextInputGotFocus(object? sender, EventArgs e)
        {
            this.KeyPreview = false;
        }

        private void Page_4_IOTable_Initialize(object sender, EventArgs e)
        {
            controlMaster.FormIntoIoTable();
            timer1.Enabled = true;
            Task.Run(() =>
            {
                Thread.Sleep(500);
                this.Invoke(new Action(() =>
                {
                    this.Focus();//设置焦点
                }));
            });
            tbEmptyMoveSpeed.Text = controlMaster.EmptyMoveSpeed.ToString();
            uiTextBox2.Text = controlMaster.GlueClosePlaneZpos.ToString();
            uiTextBox3.Text = controlMaster.SpurtGlueClosePlaneZpos.ToString();
            uiTextBox7.Text = controlMaster.GlueDefaultHeight.ToString();
            uiTextBox8.Text = controlMaster.SpurtGlueDefaultHeight.ToString();
            uiTextBox9.Text = controlMaster.ZSafePos.ToString();
            uiTextBox1.Text = controlMaster.GlueHeadDeviationX.ToString();
            uiTextBox4.Text = controlMaster.GlueHeadDeviationY.ToString();
            uiTextBox5.Text = controlMaster.SpurtGlueHeadDeviationX.ToString();
            uiTextBox6.Text = controlMaster.SpurtGlueHeadDeviationY.ToString();

        }

        private void Page_4_IOTable_Finalize(object sender, EventArgs e)
        {
            controlMaster.FormExitIoTable();
            timer1.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (controlMaster.MachineIsEmergentStop)
            {
                btEmergentStop.Enabled = false;
                btResetEmergentStop.Enabled = true;
            }
            else
            {
                btEmergentStop.Enabled = true;
                btResetEmergentStop.Enabled = false;
            }

            lbPosX.Text = controlMaster.XAxisDPos.ToString();
            lbPosY.Text = controlMaster.YAxisDPos.ToString();
            lbPosZ.Text = controlMaster.ZAxisDPos.ToString();
            lbSpeed.Text = controlMaster.Speed.ToString();
            //输入
            //System.Reflection
            //BindingFlags flagSerialNum = BindingFlags.NonPublic | BindingFlags.Instance;
            //MemberInfo[] memberInfos = thisType.GetMember("uiLedBulb0", flagSerialNum);
            //if (memberInfos == null)
            //{
            //    ShowErrorDialog("memberInfos is null");
            //    return;
            //}
            //foreach (MemberInfo memberInfo in memberInfos)
            //{
            //    if (memberInfo.MemberType == MemberTypes.Field && memberInfo.GetType() == typeof(UILedBulb))
            //    {
            //        memberInfo.
            //    }
            //}
            uiLedBulb0.On = Inputs[0].Enable;
            uiLedBulb1.On = Inputs[1].Enable;
            uiLedBulb2.On = Inputs[2].Enable;
            uiLedBulb3.On = Inputs[3].Enable;
            uiLedBulb4.On = Inputs[4].Enable;
            uiLedBulb5.On = Inputs[5].Enable;
            uiLedBulb6.On = Inputs[6].Enable;
            uiLedBulb7.On = Inputs[7].Enable;
            uiLedBulb8.On = Inputs[8].Enable;
            uiLedBulb9.On = Inputs[9].Enable;

            uiLedBulb10.On = Inputs[10].Enable;
            uiLedBulb11.On = Inputs[11].Enable;
            uiLedBulb12.On = Inputs[12].Enable;
            uiLedBulb13.On = Inputs[13].Enable;
            uiLedBulb15.On = Inputs[15].Enable;
            uiLedBulb16.On = Inputs[16].Enable;
            uiLedBulb17.On = Inputs[17].Enable;
            uiLedBulb18.On = Inputs[18].Enable;
            uiLedBulb19.On = Inputs[19].Enable;

            uiLedBulb20.On = Inputs[20].Enable;
            uiLedBulb21.On = Inputs[21].Enable;
            uiLedBulb22.On = Inputs[22].Enable;
            uiLedBulb23.On = Inputs[23].Enable;
            uiLedBulb24.On = Inputs[24].Enable;
            uiLedBulb25.On = Inputs[25].Enable;
            uiLedBulb26.On = Inputs[26].Enable;
            uiLedBulb27.On = Inputs[27].Enable;
            uiLedBulb28.On = Inputs[28].Enable;
            uiLedBulb29.On = Inputs[29].Enable;

            uiLedBulb30.On = Inputs[30].Enable;
            uiLedBulb31.On = Inputs[31].Enable;
            uiLedBulb32.On = Inputs[32].Enable;
            uiLedBulb33.On = Inputs[33].Enable;
            uiLedBulb34.On = Inputs[34].Enable;
            uiLedBulb35.On = Inputs[35].Enable;

            uiLedOut0.On = Outputs[0].Enable;
            uiLedOut1.On = Outputs[1].Enable;
            uiLedOut2.On = Outputs[2].Enable;
            uiLedOut3.On = Outputs[3].Enable;
            uiLedOut4.On = Outputs[4].Enable;
            uiLedOut5.On = Outputs[5].Enable;
            uiLedOut6.On = Outputs[6].Enable;
            uiLedOut7.On = Outputs[7].Enable;
            uiLedOut8.On = Outputs[8].Enable;
            uiLedOut9.On = Outputs[9].Enable;
            uiLedOut10.On = Outputs[10].Enable;
            uiLedOut11.On = Outputs[11].Enable;
        }

        private void Page_4_IOTable_Click(object sender, EventArgs e)
        {
            Task.Run(() =>
            {
                Thread.Sleep(10);
                this.Invoke(new Action(() =>
                {
                    this.Focus();
                    btEmergentStop.Focus();//设置焦点
                    this.KeyPreview = true;
                }));
            });
        }

        private void uiLedOut_Click(object sender, EventArgs e)
        {
            string name = ((UILedBulb)sender).Name;
            int index = int.Parse(name.Substring(8));
            Outputs[index].Enable = !Outputs[index].Enable;
        }

        private void btEmergentStop_Click(object sender, EventArgs e)
        {
            try
            {
                controlMaster.MachineEmergentStop();

            }
            catch (Exception ee)
            {

                ShowErrorDialog(ee.Message);
            }
        }

        private void btResetEmergentStop_Click(object sender, EventArgs e)
        {
            try
            {
                controlMaster.MachineResetEmergentStop();
            }
            catch (Exception ee)
            {
                ShowErrorDialog(ee.Message);
            }
        }

        private void tbEmptyMoveSpeed_TextChanged(object sender, EventArgs e)
        {
            try
            {
                controlMaster.EmptyMoveSpeed = float.Parse(((UITextBox)sender).Text);
            }
            catch
            {
                ((UITextBox)sender).Text = controlMaster.EmptyMoveSpeed.ToString();
            }
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            controlMaster.GlueClosePlaneZpos = controlMaster.ZAxisDPos;
            uiTextBox2.Text = controlMaster.GlueClosePlaneZpos.ToString();
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            controlMaster.SpurtGlueClosePlaneZpos = controlMaster.ZAxisDPos;
            uiTextBox3.Text = controlMaster.SpurtGlueClosePlaneZpos.ToString();
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            controlMaster.GlueDefaultHeight = controlMaster.GlueClosePlaneZpos - controlMaster.ZAxisDPos;
            uiTextBox7.Text = controlMaster.GlueDefaultHeight.ToString();
        }

        private void uiButton4_Click(object sender, EventArgs e)
        {
            controlMaster.SpurtGlueDefaultHeight = controlMaster.SpurtGlueClosePlaneZpos - controlMaster.ZAxisDPos;
            uiTextBox8.Text = controlMaster.SpurtGlueDefaultHeight.ToString();
        }

        private void uiTextBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                controlMaster.GlueHeadDeviationX = float.Parse(((UITextBox)sender).Text);
            }
            catch
            {
                ((UITextBox)sender).Text = controlMaster.GlueHeadDeviationX.ToString();
            }
        }

        private void uiTextBox4_TextChanged(object sender, EventArgs e)
        {
            try
            {
                controlMaster.GlueHeadDeviationY = float.Parse(((UITextBox)sender).Text);
            }
            catch
            {
                ((UITextBox)sender).Text = controlMaster.GlueHeadDeviationY.ToString();
            }
        }

        private void uiTextBox5_TextChanged(object sender, EventArgs e)
        {
            try
            {
                controlMaster.SpurtGlueHeadDeviationX = float.Parse(((UITextBox)sender).Text);
            }
            catch
            {
                ((UITextBox)sender).Text = controlMaster.SpurtGlueHeadDeviationX.ToString();
            }
        }

        private void uiTextBox6_TextChanged(object sender, EventArgs e)
        {
            try
            {
                controlMaster.SpurtGlueHeadDeviationY = float.Parse(((UITextBox)sender).Text);
            }
            catch
            {
                ((UITextBox)sender).Text = controlMaster.SpurtGlueHeadDeviationY.ToString();
            }
        }

        private void uiRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (uiRadioButton1.Checked)
            {
                controlMaster.IsSpurtGlue = true;
            }
            else
            {
                controlMaster.IsSpurtGlue = false;
            }
        }

        private void resetBtn_Click(object sender, EventArgs e)
        {
            controlMaster.AllAxisHoming();
        }

        private void uiButton5_Click(object sender, EventArgs e)
        {
            controlMaster.ZSafePos = controlMaster.ZAxisDPos;
            uiTextBox9.Text = controlMaster.ZSafePos.ToString();
        }
    }
}
